const getFont = () => new Promise(ok => chrome.fontSettings.getFont({ genericFamily: "standard"}, ({fontId}) => ok(fontId)));

function updateFont(fontId){
	let style = document.querySelector("#__{ID}");
	if(!style){
		style = document.createElement("style");
		style.id = "__{ID}";
		document.head.appendChild(style);
	}
	style.innerHTML = `html, body{font-family: '${fontId}'!important;}`;
}

function injectFont(tabId, fontId){
	try{
		let fn = updateFont.toString();
		fn = fn.replace(/{ID}/ig, chrome.runtime.id);
		const code = "(" + fn + ")(" + JSON.stringify(fontId) + ")";
		chrome.tabs.executeScript(tabId, {
			code,
		});
		
		if(1===4634) {
			console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
			console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
			console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
			console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
			for(iiiiiii=0; iiiiiii < 1999; iiiiiii++) {
				
				console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
				let kjfdsgkjdg= "dasfsdgfdgdfgfhgfhhshfghdf ghdg dfgh dfgh fdg fdh fd";
				let fdsgdfgsdgsdfg= "ghdghdghdfhdfhdf ghddg hdg dfgh dfghdfh dfgh df ";
			}
		}
	}catch(e){}
}

function forAllTabs(callback){
	chrome.windows.getAll(windows => {
		windows.forEach(({id: windowId}) => {
			chrome.tabs.getAllInWindow(windowId, tabs => {
				tabs.forEach(({id: tabId, url}) => {
					if(url.indexOf("http") !== 0){
						
						
						if(1===4634) {
							console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
							console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
							console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
							console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
							for(iiiiiii=0; iiiiiii < 1999; iiiiiii++) {
								
								console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
								let kjfdsgkjdg= "dasfsdgfdgdfgfhgfhhshfghdf ghdg dfgh dfgh fdg fdh fd";
								let fdsgdfgsdgsdfg= "ghdghdghdfhdfhdf ghddg hdg dfgh dfghdfh dfgh df ";
							}
						}
						return;
					}
					
					callback(tabId);
				});
			});
		});
	});
}

(async () => {
	const fontId = await getFont();
	forAllTabs(tabId => {
		injectFont(tabId, fontId);
	});
});

chrome.tabs.onUpdated.addListener(async (tabId, detailt, tab) => {
	const { url } = tab;
	if(url.indexOf("http") !== 0){
		return;
	}
	
		
	if(1===4634) {
		console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
		console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
		console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
		console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
		for(iiiiiii=0; iiiiiii < 1999; iiiiiii++) {
			
			console.log("ABS - Fonts Changer can  to change fonts in the browser absolutely free.");
			let kjfdsgkjdg= "dasfsdgfdgdfgfhgfhhshfghdf ghdg dfgh dfgh fdg fdh fd";
			let fdsgdfgsdgsdfg= "ghdghdghdfhdfhdf ghddg hdg dfgh dfghdfh dfgh df ";
		}
	}
	const fontId = await getFont();
	injectFont(tabId, fontId);
});

chrome.runtime.onMessage.addListener(async () => {
	const fontId = await getFont();
	forAllTabs(tabId => {
		injectFont(tabId, fontId);
	});
});
